<?php
/**
 * common model file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/12/12
 * Time: 16:52
 */

namespace HiCommon\Model;

class UsersApprise extends Base
{

    protected $table = 'users_apprise';

    protected $guarded = [];

    //public $timestamps = false;

    /**
     * 应该被调整为日期的属性
     *
     * @var array
     */
    protected $dates = [
    ];


    /**
     * 获取状态对应的名称
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getStateAttribute($value)
    {
        switch ($value) {
            case 0:
                $res = ['id' => 0, 'name' => '草稿'];
                break;
            case 1:
                $res = ['id' => 1, 'name' => '待审核'];
                break;
            case 2:
                $res = ['id' => 2, 'name' => '通过'];
                break;
            case 3:
                $res = ['id' => 3, 'name' => '拒绝'];
                break;
            default:
                $res = ['id' => -1, 'name' => '未知'];
        }
        return $res;
    }

    public function getContentAttribute($value)
    {
        return $value == '' ? '未填写评价内容' : $value;
    }

    /**
     * 扩展信息
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getExtendAttribute()
    {
        $s_created_at = $this->created_at;
        $arr_data = $this->arr_state_display_name();
        $arr_data['created_at'] = ymd_friendly_date($s_created_at);
        $arr_data['api'] = $this->table_field_api();

        return $arr_data;
    }


    /**
     * state 状态值数组
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function state_arr()
    {

        //检测状态：0无 1待检测 2检测成功 9检测失败
        $arr_check_state = [
            ['id' => -99, 'name' => '检测状态'],
            ['id' => 0, 'name' => '无'],
            ['id' => 1, 'name' => '待检测'],
            ['id' => 2, 'name' => '检测成功'],
            ['id' => 9, 'name' => '检测失败'],
        ];

        $arr_data = [
            'check_state' => $arr_check_state,
        ];
        return $arr_data;
    }

    /**
     * 获取state值和注释的集合
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function arr_state_name()
    {
        return $this->hi_arr_state_name($this->state_arr());
    }

    /**
     * 获取state值和注释的集合
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function arr_state_display_name()
    {
        return $this->hi_arr_state_display_name($this->state_arr());
    }


    //关联用户表
    public function user()
    {
        return $this->belongsTo(Users::class, 'user_id')->select('id', 'nickname', 'username', 'head_img');
    }


}